<?php if(!defined('IN_FIRADIO')) {header('HTTP/1.1 403 Forbidden');exit('<h1>Access Denied</h1>');}?>
<?php $GLOBALS['script'][] = function() {?><script>
vue.methods.postapi = function () {
    const that = this;
    that.init();
    that.ApiPost('/panel/sys/menu/list.php', that.form).then(function (data) {
        vue.data.apidata = data.data;
    }, function (data) {
    });
};
vue.methods.postapi_add = function () {
    const that = this;
    const form = {};
    form.col = that.add_col;
    that.ApiPost('/panel/sys/menu/add.php', form).then(function (data) {
        vue.methods.postapi();
    }, function (data) {
    });
};
vue.methods.postapi_save = function (col, col_key) {
    const that = this;
    const form = {};
    form.col_0 = col[0];
    form.col_key = col_key;
    form.col_val = col[col_key];
    that.ApiPost('/panel/sys/menu/save.php', form).then(function (data) {
        vue.methods.postapi();
    }, function (data) {
    });
};
vue.methods.postapi_del = function () {
    const that = this;
    if (that.del_ids.length <= 0) {
        alert('请先选择要删除的记录');
        return;
    }
    if (!confirm("确定要删除这" + that.del_ids.length + "条记录吗？")) return;
    that.ApiPost('/panel/sys/menu/del.php', {'del_ids': that.del_ids}).then(function (data) {
        vue.methods.postapi();
    }, function (data) {
    });
};
vue.methods.getDelIds = function () {
    const that = this;
    const del_ids = [];
    for (var key in that.apidata.table.cols) {
        const col = that.apidata.table.cols[key];
        if (col.del) del_ids.push(col[0]);
    }
    that.del_ids = del_ids;
};
vue.methods.init = function () {
    vue.data.disabled = false;
    vue.data.del_ids = [];
    vue.data.apidata = {};
    vue.data.add_col = {};
};
vue.methods.init();
vue.mounted = function () {
    vue.methods.postapi();
};
</script><?php }?>

<!-- Custom Tabs -->
<form v-on:submit="postapi" onsubmit="return false" :disabled="disabled">
<div class="nav-tabs-custom">

    <table class="table table-bordered table-striped dataTable" v-if="apidata.table">
        <tr align="center">
            <td v-for="field in apidata.table.fields"
                :width="field.width"
            >{{field.title}}</td>
            <td>操作</td>
        </tr>
        <tr v-for="col in apidata.table.cols" align="center">
            <td v-for="(cv,ck) in col">
                <input v-if="apidata.table.fields[ck].type=='text'" type="text" style="width:100%" v-on:change="postapi_save(col,ck)" v-model="col[ck]" />
                <label v-else-if="apidata.table.fields[ck].type=='checkbox'"><input type="checkbox" v-on:change="postapi_save(col,ck)" v-model="col[ck]" />
                    <span v-if="apidata.table.fields[ck].label">{{apidata.table.fields[ck].label}}</span>
                    <span v-else>是</span>
                </label>
                <select v-else-if="apidata.table.fields[ck].type=='select'" v-model="col[ck]" v-on:change="postapi_save(col,ck)">
                    <option value="0">无(最顶级)</option>
                    <option v-for="option in apidata.table.fields[ck].select" :value="option.id">{{option.seq}}:{{option.title}}</option>
                </select>
                <span v-else>{{cv}}</span>
            </td>
            <td><label><input type="checkbox" v-model="col.del" v-on:change="getDelIds()" />删除</label></td>
        </tr>
        <tr align="center" v-if="del_ids.length<=0">
            <td v-for="(field,fk) in apidata.table.fields">
                <span v-if="fk==0">新增</span>
                <input v-else-if="field.type=='text'" type="text" style="width:100%" v-model="add_col[fk]" />
                <select v-else-if="apidata.table.fields[fk].type=='select'" v-model="add_col[fk]">
                    <option value="0">无(最顶级)</option>
                    <option v-for="option in apidata.table.fields[fk].select" :value="option.id">{{option.seq}}:{{option.title}}</option>
                </select>
                <span v-else>-</span>
            </td>
            <td><button class="btn btn-default btn-xs" type="button" v-on:click="postapi_add()">提交新增</button></td>
        </tr>
        <tr align="center" v-if="del_ids.length>0">
            <td :colspan="apidata.table.fields.length+1">
                <button class="btn btn-danger" type="button" v-on:click="postapi_del()">删除</button>
            </td>
        </tr>
    </table>
</div>
</form>
